﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FileSplitter;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        FileSplitter.Splitter Split = new FileSplitter.Splitter();
        byte[] FileBuffer = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            Split.AddNotifyProgress(this.SetProcessBar);

          
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog odlg = new OpenFileDialog();
            odlg.Filter = "文件|*.*";
            if (odlg.ShowDialog() == DialogResult.OK)
            {
                FileBuffer = File.ReadAllBytes(odlg.FileName);
                txtFilePath.Text = odlg.FileName;
            }
            odlg.Dispose();
        }

        private void btnSaveDIR_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbdlg = new FolderBrowserDialog();
            if (fbdlg.ShowDialog() == DialogResult.OK)
            {
                this.txtSavedir.Text = fbdlg.SelectedPath;
            }
            fbdlg.Dispose();
        }

        private void btnSplit_Click(object sender, EventArgs e)
        {
            SaveParam param = new SaveParam();
            param.FloderDir = txtSavedir.Text;
            param.FileName = txtFileName.Text;
            param.FileSuffix = txtFileSuffix.Text;
            if (!Split.SetSaveInfo(param) || this.FileBuffer == null)
            {
                MessageBox.Show("参数错误！");
                return;
            }
            var ret = Split.StartSpliteFile(FileBuffer, uint.Parse(txtSpliteNU.Text));
            if (ret)
            {
                MessageBox.Show("分割成功！");
            }
            else
            {
                MessageBox.Show("失败！");
            }
            this.FileBuffer = null;
        }

        private void SetProcessBar(float val)
        {
            progressBar1.Invoke(new Action(() => { this.progressBar1.Value = (int)(val * 100); }));
        }

        private void btnCombine_Click(object sender, EventArgs e)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Users\Dell\Desktop\新建文件夹");
            FileInfo[] fileArr = dirInfo.GetFiles();
            List<byte[]> ls = new List<byte[]>();
            byte[] tempBts = new byte[1024];
            for (int i = 0; i < fileArr.Length; i++)
            {
                FileStream fs = fileArr[i].OpenRead();
                List<byte> tempLs = new List<byte>();
                while (true)
                {
                    int readNu = fs.Read(tempBts, 0, tempBts.Length);
                    if (readNu <= 0)
                    {
                        break;
                    }
                    for (int idx = 0; idx < readNu; idx++)
                    {
                        tempLs.Add(tempBts[idx]);
                    }
                }
                fs.Close();
                ls.Add(tempLs.ToArray());
            }

            SaveParam param = new SaveParam();
            param.FloderDir = txtSavedir.Text;
            param.FileName = txtFileName.Text;
            param.FileSuffix = txtFileSuffix.Text;
            if (!Split.SetSaveInfo(param) )
            {
                MessageBox.Show("参数错误！");
                return;
            }
            var ret = Split.CombineFiles(ls);
            if (ret)
            {
                MessageBox.Show("合并成功");
            }
            else
            {
                MessageBox.Show("合并失败！");
            }
        }
    }


}
